// pages/wechatPay/wechatPay.js
const utils = require('../../utils/util.js');
const post = utils.post;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        uid: '',
        shoppCount: '',
        name: '',
        ntype: '',
        show_success: false,
        pay_result: '',
        paying: false,
        ifad: 1,
        show_info_img: '',
        show_info_url: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;

        // 广告切换
        let p = {
            m: 'show_info'
        };
        post(p).then(res => {
            if (res.status == 2) {
                that.setData({
                    ifad: res.status,
                    show_info_img: res.show_info_img,
                    show_info_url: res.show_info_url
                })
            }
        })

        if (options.scene) {
            let _scene = decodeURIComponent(options.scene).split(':');
            that.data.uid = _scene[0];
            that.data.shoppCount = _scene[1];
            that.setData({
                name: '公众号',
                ntype: 2
            });
        } else {
            if (options.uid && options.spc) {
                that.data.uid = options.uid;
                that.data.shoppCount = options.spc
                that.setData({
                    name: 'APP',
                    ntype: 1
                });
            }
        }
    },

    z_check() {
        let that = this;
        if (that.data.uid) {
            let p = {
                m: 'z_check',
                uid: that.data.uid,
                jump_weapp: utils.appid
            };

            post(p).then(res => {
                // console.log(res);
                if (0 == res.r) {
                    if (1 == res.z_check) {
                        if (!that.data.paying) {
                            that.gopay();
                        }
                    } else {
                        that.goauth();
                    }
                }
            });
        } else {
            wx.showToast({
                title: '非法访问'
            })

            that.goback();
        }
    },

    goauth() {
        let that = this;
        wx.navigateTo({
            url: '/pages/auth/auth?uid=' + that.data.uid + '&from=xpay',
        })
    },

    gopay() {
        let that = this;
        if (!!(that.data.uid) && !!(that.data.shoppCount)) {
            wx.showLoading({
                title: '正在发起支付',
            });

            let payUrlAcitve = utils.domain + '/web/lib/sign_pays/pay.php';
            let p = {
                uid: that.data.uid,
                partake_count: that.data.shoppCount,
                jump_weapp: utils.appid
            }

            that.data.paying = true;
            post(p, payUrlAcitve).then(res => {
                // console.log(res);

                let paySuccess = {
                    m: "ifpay",
                    out_trade_no: res.out_trade_no
                }

                wx.hideLoading();
                wx.requestPayment({
                    timeStamp: res.pay_info.timeStamp,
                    nonceStr: res.pay_info.nonceStr,
                    package: res.pay_info.package,
                    signType: res.pay_info.signType,
                    paySign: res.pay_info.paySign,
                    success: function(result) { // 调用支付成功
                        wx.showLoading({
                            title: '支付确认中',
                        })
                        setTimeout(function() {
                            post(paySuccess, utils.domain + "/activity.php").then(res => {
                                if (res.pay == "1") {
                                    that.setData({
                                        show_success: true,
                                        pay_result: '恭喜您支付成功！',
                                        uid: '',
                                        shoppCount: '',
                                        paying: false
                                    });
                                }

                                wx.hideLoading();
                            })
                        }, 2000);
                    },
                    fail: function(result) { // 用户取消支付
                        if ('requestPayment:fail cancel' == result.errMsg) {
                            that.setData({
                                show_success: true,
                                pay_result: '您取消支付！'
                            });
                        } else {
                            that.setData({
                                show_success: true,
                                pay_result: '调用支付失败！'
                            });
                        }
                    },
                    complete: function(result) {

                    }
                })
            });
        } else {
            wx.showToast({
                title: '非法访问'
            })

            that.goback();
        }
    },

    goback() {
        // -1代表直接关闭本页面
        wx.navigateBack({
            delta: -1,
        })
    },

    launchAppError: function(e) {
        this.setData({
            errmsg: e.detail.errMsg
        });
        wx.showModal({
            title: '错误提示',
            content: e.detail.errMsg,
            showCancel: false,
            success: function(res) {}
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        if (that.data.uid && that.data.shoppCount) {
            that.z_check();
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    /*onShareAppMessage: function () {

    }*/
    submitFormId(e) {
        let formId = e.detail.formId;
        let that = this;
        let button = e.detail.target.dataset.name;
        let page = getCurrentPageUrlWithArgs();
        let p = {
            m: 'colform',
            uid: that.data.uid,
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId
        };
        if (page && page.length > 0) {
            p.page = page;
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        // console.log(p)
        post(p).then(res => {})
    }
})